package Thread;

public class ThreadDemo12 {
    public static void main(String[] args) {
    Object object=new Object();

        Thread thread1=new Thread(()->{
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("wait之前");
            synchronized (object){
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            System.out.println("wait之后");
        });
        Thread thread2=new Thread(()->{
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("notify之前");

           synchronized (object) {      //这个加锁的目的是因为这个线程要接手上面线程的活，上面那个线程因为
                                      //因为wait之后，已经释放锁了，所以可以获取了，此时上面那个线程的状态因为wait
                                     //所以处于阻塞等待的状态，notify就可以唤醒上面的进程，通过锁对象
               object.notify();      //总得来说notify就是随机唤醒一个线程
           }
            System.out.println("notify之后");
        });
        thread1.start();
        thread2.start();

    }
}
